package acheng1314.cn.a3dbuild2.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.acheng.achengutils.utils.StringUtils;

import java.util.ArrayList;

import acheng1314.cn.a3dbuild2.R;
import acheng1314.cn.a3dbuild2.bean.ProjectItemBean;

/**
 * Created by Administrator on 2016/10/6 0006.
 */

public class ProjectListAdapter extends BaseAdapter {

    private final ArrayList<ProjectItemBean.ResultBean> mData;
    private final Context context;

    public ProjectListAdapter(ArrayList<ProjectItemBean.ResultBean> data, Context context) {
        this.mData = data;
        this.context = context;
    }


    public void update(ArrayList<ProjectItemBean.ResultBean> mListData) {
        mData.clear();
        mData.addAll(mListData);
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mData.isEmpty() ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.isEmpty() ? null : mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mData.isEmpty() ? 0 : position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            LayoutInflater mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(R.layout.item_project, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //init data
        holder.mTv_project_name.setText(mData.get(position).getName());
        holder.mTv_project_introduce.setText(mData.get(position).getIntro());

        //1 未提交，2 待审核，3 监督中，4 待验收，5 已完工
        switch ((int) StringUtils.toLong(mData.get(position).getState())) {
            case 1:
                holder.mTv_project_img.setText("本项目还未提交");
                break;
            case 2:
                holder.mTv_project_img.setText("本项目还待审核");
                break;
            case 3:
                holder.mTv_project_img.setText("项目监督中");
                break;
            case 4:
                holder.mTv_project_img.setText("本项目待验收");
                break;
            case 5:
                holder.mTv_project_img.setText("本项目已完工");
                break;
            default:
                holder.mTv_project_img.setText("本项目属性未知");
                break;
        }

        return convertView;
    }

    private class ViewHolder {
        private final View rootView;
        private final TextView mTv_project_name;
        private final TextView mTv_project_introduce;
        private final TextView mTv_project_img;

        ViewHolder(View rootView) {
            this.rootView = rootView;
            this.mTv_project_name = (TextView) rootView.findViewById(R.id.mTv_project_name);
            this.mTv_project_introduce = (TextView) rootView.findViewById(R.id.mTv_project_introduce);
            this.mTv_project_img = (TextView) rootView.findViewById(R.id.mTv_project_img);
        }
    }


}
